/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * NetworkPanel.java
 *
 * Created on 26 juin 2010, 13:43:20
 */
package darknet.gui.wizard.firstrun;

import java.awt.event.KeyEvent;
import java.net.InetAddress;
import java.net.UnknownHostException;
import javax.swing.SpinnerNumberModel;
import javax.swing.SwingWorker;

/**
 *
 * @author Yoyo
 */
public class NetworkPanel extends javax.swing.JPanel {

    /** Reference to the wizard Dialog. */
    private WizardFirstRunDialog wizard;

    private boolean isAlreadyGenerated = false;

    /** Creates new form NetworkPanel */
    public NetworkPanel(WizardFirstRunDialog wizard) {
        this.wizard = wizard;
        initComponents();
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        wizardIcon = new javax.swing.JLabel();
        jScrollPane2 = new javax.swing.JScrollPane();
        jEditorPane1 = new javax.swing.JEditorPane();
        btnNext = new javax.swing.JButton();
        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        textFieldAddress = new javax.swing.JTextField();
        btnPrevious = new javax.swing.JButton();
        spinnerPort = new javax.swing.JSpinner();
        labelFeedback = new javax.swing.JLabel();

        wizardIcon.setIcon(new javax.swing.ImageIcon(getClass().getResource("/darknet/gui/wizard/firstrun/wizard.png"))); // NOI18N

        jEditorPane1.setContentType("text/html");
        jEditorPane1.setEditable(false);
        jEditorPane1.setText("<html>\r\n  <head>\r\n  </head>\r\n  <body>\r\n  <h3>Network configuration (2/3)</h3>\n  <p>\nNow, you have to enter your IP Internet Address (not your local address!) and a port.<br />\nYou can choose the default port if you wish.<br />\n<br />\nIf you are behind a router, you can use NAT to forward the traffic onto your computer\n\t\n  </p>\n  </body>\r\n</html>\r\n");
        jScrollPane2.setViewportView(jEditorPane1);

        btnNext.setText("Next");
        btnNext.setMaximumSize(new java.awt.Dimension(73, 23));
        btnNext.setMinimumSize(new java.awt.Dimension(73, 23));
        btnNext.setPreferredSize(new java.awt.Dimension(73, 23));
        btnNext.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnNextActionPerformed(evt);
            }
        });

        jLabel1.setText("Address :");

        jLabel2.setText("Port : ");

        textFieldAddress.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyReleased(java.awt.event.KeyEvent evt) {
                textFieldAddressKeyReleased(evt);
            }
        });

        btnPrevious.setText("Previous");
        btnPrevious.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnPreviousActionPerformed(evt);
            }
        });

        spinnerPort.setModel(new javax.swing.SpinnerNumberModel(1337, 1, 65536, 1));

        labelFeedback.setForeground(new java.awt.Color(204, 0, 0));

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(wizardIcon)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addGroup(layout.createSequentialGroup()
                        .addGap(23, 23, 23)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                            .addGroup(layout.createSequentialGroup()
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addComponent(jLabel2)
                                    .addComponent(jLabel1))
                                .addGap(34, 34, 34)
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addGroup(layout.createSequentialGroup()
                                        .addComponent(spinnerPort, javax.swing.GroupLayout.PREFERRED_SIZE, 67, javax.swing.GroupLayout.PREFERRED_SIZE)
                                        .addGap(18, 18, 18)
                                        .addComponent(labelFeedback, javax.swing.GroupLayout.DEFAULT_SIZE, 247, Short.MAX_VALUE))
                                    .addComponent(textFieldAddress, javax.swing.GroupLayout.DEFAULT_SIZE, 332, Short.MAX_VALUE)))
                            .addComponent(jScrollPane2, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.PREFERRED_SIZE, 412, javax.swing.GroupLayout.PREFERRED_SIZE)))
                    .addGroup(layout.createSequentialGroup()
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(btnPrevious, javax.swing.GroupLayout.PREFERRED_SIZE, 98, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(btnNext, javax.swing.GroupLayout.PREFERRED_SIZE, 94, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(wizardIcon)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE, 203, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(textFieldAddress, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(jLabel1))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jLabel2)
                            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                                .addComponent(spinnerPort, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addComponent(labelFeedback)))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(btnPrevious, javax.swing.GroupLayout.PREFERRED_SIZE, 42, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(btnNext, javax.swing.GroupLayout.PREFERRED_SIZE, 42, javax.swing.GroupLayout.PREFERRED_SIZE))))
                .addGap(17, 17, 17))
        );
    }// </editor-fold>//GEN-END:initComponents

    private void next() {

        btnNext.setEnabled(false);
        btnNext.setText("Checking ...");
        new SwingWorker<Object, Object>() {

            @Override
            protected Object doInBackground() {

                if (textFieldAddress.getText().length() != 0) {

                    boolean isValidAddress = false;
                    String address = textFieldAddress.getText();
                    try {
                        InetAddress.getByName(address);
                        isValidAddress = true;
                    } catch (UnknownHostException ex) {
                    }

                    if (!isValidAddress) {
                        btnNext.setText("Next");
                        labelFeedback.setText("Invalid Address");
                        btnNext.setEnabled(false);
                        return null;
                    }

                    btnNext.setEnabled(true);
                    btnNext.setText("Next");
                    labelFeedback.setText("");

                    NetworkPanel.this.wizard.setAddress(address);
                    int port = ((SpinnerNumberModel) spinnerPort.getModel())
                            .getNumber().intValue();
                    NetworkPanel.this.wizard.setPort(port);

                    NetworkPanel.this.wizard.showGenerate();

                    if (!isAlreadyGenerated) {
                        NetworkPanel.this.wizard.getGeneratePanel().start();
                        isAlreadyGenerated = true;
                    }

                    
                } else {
                    btnNext.setText("Next");
                    labelFeedback.setText("Please check your address");
                    btnNext.setEnabled(true);
                }

                return null;
            }
        }.execute();
    }

    private void btnNextActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnNextActionPerformed
        next();
}//GEN-LAST:event_btnNextActionPerformed

    private void btnPreviousActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnPreviousActionPerformed
        wizard.showUserAndPassword();
}//GEN-LAST:event_btnPreviousActionPerformed

    private void textFieldAddressKeyReleased(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_textFieldAddressKeyReleased
        btnNext.setEnabled(true);
        labelFeedback.setText("");

        if (evt.getKeyCode() == KeyEvent.VK_ENTER) {
            next();
        }
    }//GEN-LAST:event_textFieldAddressKeyReleased

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnNext;
    private javax.swing.JButton btnPrevious;
    private javax.swing.JEditorPane jEditorPane1;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JScrollPane jScrollPane2;
    private javax.swing.JLabel labelFeedback;
    private javax.swing.JSpinner spinnerPort;
    private javax.swing.JTextField textFieldAddress;
    private javax.swing.JLabel wizardIcon;
    // End of variables declaration//GEN-END:variables
}
